package com.googlecode.maratische.google.gui;

import static com.googlecode.maratische.google.MainController.getMainController;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.googlecode.maratische.google.BaseException;
import com.googlecode.maratische.google.DaoAction;
import com.googlecode.maratische.google.MainController;
import com.googlecode.maratische.google.listeners.SpinnerListener;
import com.googlecode.maratische.google.model.Command;
import com.googlecode.maratische.google.model.Item;

public class CommandLogFrame extends JFrame implements SpinnerListener, MouseListener {
	
	private JPopupMenu commandsPopup = null;
	private JMenuItem menuDeleteCommand = null;
	
	private JTable table = null;
	private LogTableModel tableModel = null;
	public CommandLogFrame() {
		setSize(500, 500);
		setTitle("Commands");
		setLayout(new GridLayout(1, 1));
//		add(new JLabel("TEXT"));
		tableModel = new LogTableModel();
		table = new JTable(tableModel);
		add(new JScrollPane(table));
		setVisible(true);
		updateData();
//		table.updateUI();
		MainController.getMainController().addObserverListener((SpinnerListener)this);
		
		initPopupMenu();
	}

	private void initPopupMenu() {
		commandsPopup = new JPopupMenu();
		menuDeleteCommand = new JMenuItem(getMainController().getBundleMessage("commandLogFrame.item.deleteCommand"));
		commandsPopup.add(menuDeleteCommand);
		menuDeleteCommand.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Command command = tableModel.getCommand(table.getSelectedRow());
				if (command != null) {
					try {
						DaoAction.getDaoAction().getCommandDao().remove(command);
					} catch (BaseException e1) {
						getMainController().showError(e1);
					}
					updateData();
				}
			}
		});
		table.addMouseListener(this);
	}

	public void spinnerDisable() {
		updateData();;
	}

	public void spinnerEnable() {
		updateData();;
	}
	private void updateData() {
		try {
			tableModel.updateData();
		} catch (SQLException e) {
			MainController.getMainController().showError("Ошибка обновления списка комманда", e);
		} catch (BaseException e) {
			MainController.getMainController().showError("Ошибка обновления списка комманда", e);
		}
		table.updateUI();
	}
	
    public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
    }

    public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
        	commandsPopup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
	
	class LogTableModel extends AbstractTableModel {

		private String[] columns = new String[] {"N", "Type","Priority", "String1","String2","Integer1","Status"};
		List<Command> commands = new ArrayList<Command>();
		
		public void updateData() throws SQLException, BaseException {
			commands.clear();
			commands.addAll(DaoAction.getDaoAction().getCommandDao().listByPriority());
		}
		
		public Command getCommand(int rowIndex) {
			if (rowIndex >-1 && rowIndex < commands.size()) {
				return commands.get(rowIndex);
			}
			return null;
		}
		
		@Override
		public String getColumnName(int column) {
			return columns[column];
		}
		
		@Override
		public int getColumnCount() {
			return columns.length;
		}

		@Override
		public int getRowCount() {
			return commands.size();
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			Command command = commands.get(rowIndex);
			switch (columnIndex) {
			case 0 : return command.getId();
			case 1 : return command.getType();
			case 2 : return command.getPriority();
			case 3 : return (command.getString1() != null) ? command.getString1() : "";
			case 4 : return (command.getString2() != null) ? command.getString2() : "";
			case 5 : return (command.getInteger1() != null) ? command.getInteger1() : "";
			case 6 : return "";
			}
			return null;
		}
		
		private static final long serialVersionUID = -4662262481606114581L;
	}
	
	private static final long serialVersionUID = -5505234058287548249L;

	public void mouseClicked(MouseEvent e) {
	}
	public void mouseEntered(MouseEvent e) {
	}
	public void mouseExited(MouseEvent e) {
	}
}
